using System;
using System.Net.Mail;
using System.Text;

namespace Glpt.Common
{
    /// <summary>
    /// 邮件类
    /// </summary>
    public class MailInfo
    {
        private static Encoding encoding = Encoding.UTF8;
        private SmtpClient stmp = null;
        private MailAddress mailsendaddr = null;

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        /// <param name="Smtp">Smtp地址</param>
        /// <param name="Port">Smtp端口</param>
        /// <param name="SendMailAddr">发送邮件地址</param>
        /// <param name="Username">用户名</param>
        /// <param name="Password">密码</param>
        public MailInfo(string Smtp, int Port, string SendMailAddr, string Username, string Password)
        {
            stmp = new SmtpClient(Smtp, Port);
            stmp.Credentials = new System.Net.NetworkCredential(Username, Password);
            this.mailsendaddr = GetMailAddress(SendMailAddr);
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="RecvMailAddr">接收邮件地址</param>
        /// <param name="CcMailAddr">抄送邮件地址</param>
        /// <param name="BccMailAddr">密抄邮件地址</param>
        /// <param name="Subject">标题</param>
        /// <param name="Body">内容</param>
        /// <param name="IsHtml">是否HTML格式</param>
        /// <param name="FileName">附件</param>
        public void Send(string RecvMailAddr, string CcMailAddr, string BccMailAddr, string Subject, string Body, bool IsHtml, string FileName)
        {
            MailMessage mail = new MailMessage();
            mail.From = mailsendaddr;
            string[] recaddrs = RecvMailAddr.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string recaddr in recaddrs)
            {
                if (recaddr.Trim().Length > 0)
                    mail.To.Add(GetMailAddress(recaddr));
            }
            string[] ccaddrs = CcMailAddr.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string ccaddr in ccaddrs)
            {
                if (ccaddr.Trim().Length > 0)
                    mail.CC.Add(GetMailAddress(ccaddr));
            }
            string[] bccaddrs = BccMailAddr.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string bccaddr in bccaddrs)
            {
                if (bccaddr.Trim().Length > 0)
                    mail.Bcc.Add(GetMailAddress(bccaddr));
            }
            mail.SubjectEncoding = encoding;
            mail.Subject = Subject;
            mail.BodyEncoding = encoding;
            mail.Body = Body;
            mail.IsBodyHtml = IsHtml;
            string[] filenames = FileName.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string file in filenames)
            {
                Attachment atta = new Attachment(file);
                string name = System.IO.Path.GetFileName(file);
                string base64 = Convert.ToBase64String(encoding.GetBytes(name));
                atta.ContentDisposition.FileName = string.Format("=?utf-8?B?{0}?=", base64);
                atta.Name = "attachment";
                atta.NameEncoding = encoding;
                mail.Attachments.Add(atta);
            }
            try
            {
                stmp.Send(mail);
            }
            catch (Exception ex)
            {
                throw new Exception("发送邮件错误：" + ex.Message);
            }
            finally
            {
                mail.Attachments.Clear();
                mail.Dispose();
            }
        }

        /// <summary>
        /// 获取邮箱地址对象
        /// </summary>
        /// <param name="mailaddrname">邮箱地址</param>
        /// <returns>邮箱地址</returns>
        public static MailAddress GetMailAddress(string mailaddrname)
        {
            MailAddress mailrecvaddr = null;
            int mlidx = mailaddrname.IndexOf(":");
            if (mlidx < 0)
                mailrecvaddr = new MailAddress(mailaddrname);
            else
                mailrecvaddr = new MailAddress(mailaddrname.Substring(mlidx + 1).Trim(), mailaddrname.Substring(0, mlidx).Trim(), encoding);
            return mailrecvaddr;
        }

        /// <summary>
        /// 释放内存
        /// </summary>
        public void Dispose()
        {
            try
            {
                stmp = null;
                GC.Collect();
            }
            catch { }
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~MailInfo()
        {
            Dispose();
        }
    }
}
